package com.atguigu.flink0922.chapter05.transform;

import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/3/2 16:09
 */
public class Flink07_Connect {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);
        
        final DataStreamSource<Integer> s1 = env.fromElements(1, 2, 3, 4);
        final DataStreamSource<String> s2 = env.fromElements("10", "20", "30", "40", "50");
    
        final ConnectedStreams<Integer, String> cs = s1.connect(s2);
    
        //cs.getFirstInput().print();
        cs
            .process(new CoProcessFunction<Integer, String, String>() {
                
                @Override
                public void processElement1(Integer value, Context ctx, Collector<String> out) throws Exception {
                    out.collect(value + " one");
                }
            
                @Override
                public void processElement2(String value, Context ctx, Collector<String> out) throws Exception {
                    out.collect(value + " two");
                }
            })
            .print();
    
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
